﻿
define(function (require) {
    require('utility/datetime');

    var service = require('utility/service');
    var utility = require('utility/utility');
    
    window.Feed = Backbone.Model.extend({
        isAvailable: true,
        initialize: function () {
            if (!this.get("CreateOnNiceTime")) {
                this.set({ "CreateOnNiceTime":
                    Date.fromTicks(this.get("UpdateOn")).format("HH:mm")
                });
            }
            if (!this.get("FullDateAndDay")) {
                this.set({ "FullDateAndDay":
                    Date.fromTicks(this.get("UpdateOn")).toFullDateAndDay()
                });
            }
            if (!this.get("NiceTime")) {
                this.set({ "NiceTime":
                    Date.fromTicks(this.get("UpdateOn")).toNiceTime()
                });
            }
            if (!this.get("Title")) {
                this.set({ "Title":
                    this.merageFeedData(this.get("TransformedData").Title)
                });
            }
            if (!this.get("Body")) {
                this.set({ "Body":
                    this.merageFeedData(this.get("TransformedData").Body)
                });
            }
        },
        merageFeedData: function (content) {
            _.templateSettings = {
                interpolate: /\{(.+?)\}/g
            };
            var data = {};
            if (this.get('FeedData')) {
                var feedData = utility.ToObject(this.get('FeedData'));
                data = $.extend(data, feedData);
            }
            if (this.get('PersonalData')) {
                var personalData = utility.ToObject(this.get('PersonalData'));
                data = $.extend(data, personalData);
            }
            var result = _.template(content)(data);
            return result;
        },
    });
});
